package Android.Billarm;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.util.Log;
import java.text.SimpleDateFormat;

public class BillScreen extends Activity {

    static final int REQUEST_MONEY=1;
    static final int REQUEST_ICONS=2;
    static final int ID_DATEPICKER_DIALOG=3;
    static final int ID_TIMEPICKER_DIALOG=4;
    Bill bill = null;
    boolean deletep;

    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle state)
    {
        super.onCreate(state);
        setContentView(R.layout.bill_info);

        if (null == bill)
        {
            deletep = false;
            bill = (Bill) getIntent() .getSerializableExtra("bill");
        }

        if (bill.dirtyp)
        {
            DB_SQLite db = new DB_SQLite (this);
            db.open ();
            Bill newbill = db.fetchBill (bill.id);

            if (newbill == null)  bill.id = -1;
            else  bill = newbill;

            db.close ();
        }
        
        setMoneyField ();
        setDateField ();
        setTimeField ();
        setActiveField ();
        setDeleteField ();
        setImageArea ();
    }

    protected void onSaveInstanceState (Bundle state)
    {
        state.putSerializable ("bill", bill);
        state.putBoolean ("deletep", deletep);
    }

    protected void onRestoreInstanceState (Bundle state)
    {
        bill = (Bill) state.getSerializable ("bill");
        deletep = state.getBoolean ("deletep");
    }

    /*-- Amount --*/
    private void setMoneyField ()
    {
        TextView amount = (TextView) findViewById(R.id.billAmount);
        amount.setText("$" + bill.getAmountString());
    }

    public void moneyClicked (View v)
    {
        Intent i = new Intent (v.getContext(), MoneyScreen.class);
        i.putExtra ("bill", bill);
        startActivityForResult (i, REQUEST_MONEY);
    }


    /*-- Date --*/
    private void setDateField ()
    {
        SimpleDateFormat d = new SimpleDateFormat ("EEE, MMM d, yyyy");
        ((TextView) findViewById(R.id.billDate))
            .setText (d.format (bill.getBillarmDate() .getTime()));
    }

    public void dateClicked (View v) { showDialog (ID_DATEPICKER_DIALOG); }

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
    {
        public void onDateSet(DatePicker view, int year, int month, int day)
        {
            bill.getBillarmDate ()
                .set (year, month, day);
            setDateField ();
        } 
    };


    /*-- Time --*/
    private void setTimeField ()
    {
        SimpleDateFormat d = new SimpleDateFormat ("h:mm a");
        ((TextView) findViewById(R.id.billTime))
            .setText (d.format (bill.getBillarmDate() .getTime()));
    }

    public void timeClicked (View v) { showDialog (ID_TIMEPICKER_DIALOG); }

    private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener()
    {
        public void onTimeSet(TimePicker view, int hour, int minute)
        {
            bill.getBillarmDate ()
                .setTimeOfDay (hour, minute);
            setTimeField ();
        } 
    };


    /*-- Active --*/
    private void setActiveField ()
    {
        TextView v = (TextView) findViewById(R.id.billActive);
        v.setText(bill.activep ? "YES" : "NO");
    }

    public void activeClicked (View v)
    {
        bill.activep = ! bill.activep;
        setActiveField ();
    }


    /*-- Delete --*/
    public void setDeleteField ()
    {
        ((TextView) findViewById (R.id.billDelete))
            .setText (deletep ? "Undelete" : "Delete");
    }
    public void deleteClicked (View v)
    {
        deletep = ! deletep;
        setDeleteField ();
    }


    /*-- Icons --*/
    private void setImageArea ()
    {
        LinearLayout imageArea = (LinearLayout) findViewById(R.id.imageArea);
        imageArea.removeAllViews ();

        if (bill.icons.size() == 0)
        {
            TextView text = new TextView (this);
            text.setText ("Add some icons!");
            imageArea.addView (text);
            return;
        }

        for (int i : bill.icons)
        {
        	ImageView image = new ImageView(getBaseContext());
            BillIcon.setImageView (i, image);
        	imageArea.addView(image);
        }
    }

    public void iconsClicked (View v)
    {
        Intent i = new Intent (v.getContext(), IconScreen.class);
        i.putExtra ("bill", bill);
        startActivityForResult (i, REQUEST_ICONS);
    }

    /*-- Done editing/viewing buttons --*/
    public void doneClicked (View v)
    {
        Intent ret = new Intent ();
        if (v.getId() != R.id.commit || (deletep && bill.id < 0))
        {
            setResult (RESULT_CANCELED, ret);
            finish ();
            return;
        }

        DB_SQLite db = new DB_SQLite (this);
        db.open ();

        if (deletep)
        {
            if (0 <= bill.id)  db.deleteBillarm (bill.id);
        }
        else
        {
            if (bill.id < 0)
                bill.id = db.insertBillarm (bill);
            else          db.updateBillarm (bill);

            ret.putExtra ("bill", bill);
            if (bill.activep)  Billarm.setAlarm (this, bill);
        }

        db.close ();

        Billarm.resetAlarm (this);

        setResult (RESULT_OK, ret); /* User committed changes */
        finish ();
    }

    /* Callback after child screen finishes */
    protected void onActivityResult (int req, int res, Intent data)
    {
        if (res != RESULT_OK)  return;
        bill = (Bill) data.getSerializableExtra("bill");
        switch (req)
        {
            case REQUEST_MONEY: setMoneyField (); break;
            case REQUEST_ICONS: setImageArea (); break;
            default: return;
        }
    }

    /* Dialog creation */
	@Override
    protected Dialog onCreateDialog(int id) {
        switch(id){
            case ID_DATEPICKER_DIALOG:
                return new DatePickerDialog(this,
                        dateSetListener,
                        bill.getBillarmDate().get (DBISODate.YEAR),
                        bill.getBillarmDate().get (DBISODate.MONTH),
                        bill.getBillarmDate().get (DBISODate.DAY_OF_MONTH));
            case ID_TIMEPICKER_DIALOG:
                return new TimePickerDialog(this,
                        timeSetListener,
                        bill.getBillarmDate().get (DBISODate.HOUR_OF_DAY),
                        bill.getBillarmDate().get (DBISODate.MINUTE),
                        false);
            default:
                return null;
        }
    }
}

